package com.chuangjiangx.member.score.ddd.domain.repository;


import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.score.ddd.dal.mapper.MbrScoreGiftRuleDalMapper;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGiftRuleId;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGiftRule;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGiftRuleScope;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGiftRuleMapper;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRule;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRuleExample;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 积分兑换规则资源库
 */
@Component
public class MbrScoreGiftRuleRepository implements Repository<MbrScoreGiftRule, MbrScoreGiftRuleId> {

    @Autowired
    private InMbrScoreGiftRuleMapper ruleMapper;
    @Autowired
    private MbrScoreGiftRuleDalMapper mbrScoreGiftRuleDalMapper;


    @Override
    public MbrScoreGiftRule fromId(MbrScoreGiftRuleId mbrScoreGiftRuleId) {
        Objects.requireNonNull(mbrScoreGiftRuleId);
        InMbrScoreGiftRule giftRule = ruleMapper.selectByPrimaryKey(mbrScoreGiftRuleId.getId());
        if (giftRule != null) {
            return convertToEntity(giftRule);
        }
        return null;
    }

    @Override
    public void update(MbrScoreGiftRule mbrMbrScoreGiftRule) {
        Objects.requireNonNull(mbrMbrScoreGiftRule);
        InMbrScoreGiftRule inMbrScoreGiftRule = convertToIn(mbrMbrScoreGiftRule);
        inMbrScoreGiftRule.setUpdateTime(new Date());
        ruleMapper.updateByPrimaryKeySelective(inMbrScoreGiftRule);
    }

    @Override
    public void save(MbrScoreGiftRule mbrMbrScoreGiftRule) {
        Objects.requireNonNull(mbrMbrScoreGiftRule);
        InMbrScoreGiftRule in = convertToIn(mbrMbrScoreGiftRule);
        Date endTime = DateUtils.setHours(in.getEndTime(), 23);
        endTime = DateUtils.setMinutes(endTime,59);
        endTime = DateUtils.setSeconds(endTime,59);
        in.setEndTime(endTime);
        ruleMapper.insertSelective(in);
        mbrMbrScoreGiftRule.setId(new MbrScoreGiftRuleId(in.getId()));
    }

    public int countByMerchantId(MerchantId merchantId) {
        InMbrScoreGiftRuleExample example = new InMbrScoreGiftRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andEnableEqualTo(Enable.ENABLE.value);
        List<InMbrScoreGiftRule> inScoreGifts = ruleMapper.selectByExample(example);
        return inScoreGifts.size();
    }

    public void updateGoodsInventory(MbrScoreGiftRule oldRule,Integer newTotalInventory) {

        InMbrScoreGiftRule inRule = convertToIn(oldRule);
        inRule.setTotalGiftLimit(newTotalInventory);

        InMbrScoreGiftRuleExample example = new InMbrScoreGiftRuleExample();
        example.createCriteria()
                .andIdEqualTo(oldRule.getId().getId())
                .andTotalGiftLimitEqualTo(oldRule.getTotalGiftLimit());

        ruleMapper.updateByExampleSelective(inRule,example);
    }

    public void accumulateCount(Long id){
        mbrScoreGiftRuleDalMapper.accumulateCount(id);
    }

    private InMbrScoreGiftRule convertToIn(MbrScoreGiftRule rule) {
        InMbrScoreGiftRule inMbrScoreGiftRule = new InMbrScoreGiftRule();
        inMbrScoreGiftRule.setName(rule.getName());
        inMbrScoreGiftRule.setGoodsName(rule.getGoodsName());
        inMbrScoreGiftRule.setGoodsPrice(rule.getGoodsPrice());
        inMbrScoreGiftRule.setScope(rule.getScope() == null ? null : rule.getScope().value);
        inMbrScoreGiftRule.setEnable(rule.getEnable() == null ? null : rule.getEnable().value);
        inMbrScoreGiftRule.setCreateTime(new Date());
        inMbrScoreGiftRule.setUpdateTime(rule.getUpdateTime());
        inMbrScoreGiftRule.setImageUrl(rule.getImageUrl());
        inMbrScoreGiftRule.setCount(rule.getCount());
        inMbrScoreGiftRule.setMerchantId(rule.getMerchantId().getId());
        inMbrScoreGiftRule.setScore(rule.getScore());
        inMbrScoreGiftRule.setExchangeLimit(rule.getExchangeLimit());
        inMbrScoreGiftRule.setRemark(rule.getRemark());
        inMbrScoreGiftRule.setTotalGiftLimit(rule.getTotalGiftLimit());
        inMbrScoreGiftRule.setStartTime(rule.getStartTime());
        inMbrScoreGiftRule.setEndTime(rule.getEndTime());
        inMbrScoreGiftRule.setId(rule.getId() == null ? null : rule.getId().getId());
        return inMbrScoreGiftRule;
    }


    private MbrScoreGiftRule convertToEntity(InMbrScoreGiftRule inRule) {
        return new MbrScoreGiftRule(
                new MbrScoreGiftRuleId(inRule.getId()), inRule.getName(), inRule.getScore(),
                inRule.getGoodsName(), inRule.getGoodsPrice(), MbrScoreGiftRuleScope.getScoreGiftScope(inRule.getScope()),
                Enable.getEnable(inRule.getEnable()), inRule.getCreateTime(), inRule.getUpdateTime(),
                new MerchantId(inRule.getMerchantId()), inRule.getImageUrl(), inRule.getCount(),
                inRule.getExchangeLimit(),inRule.getRemark(),inRule.getTotalGiftLimit(),inRule.getStartTime(),inRule.getEndTime());
    }
}
